package domain.controller;

import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

import domain.service.Utils;

@SuppressWarnings("serial")
public class ContactServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html");
		resp.getWriter().println("Hello, world");
		
		String[] check = new String[4];
		req.setAttribute("check", check);
		req.setAttribute("title", "Contact us");
		req.setAttribute("description", "Contact us  "+Utils.ValidDomain()+" - A Free SEO tool that provides free website analysis, traffic details, rankings, contact email ,similar site, same owner site , same ip site, optimization and promotion reports.");
		req.setAttribute("keyword", "contact,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info");
		
		try {
			req.getRequestDispatcher("/contact.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		resp.setContentType("text/html");

		String[] check = new String[4];
		
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		String name = req.getParameter("name");
		String email = req.getParameter("email");
		String text = req.getParameter("message");

		String msgBody = text;

		if(name == null || (name != null && name.equals("")))
	    {
	    	check[0] = "Name is not empty";
	    }
	    java.util.regex.Pattern p = java.util.regex.Pattern.compile(".+@.+\\.[a-z]+");
	    Matcher m = p.matcher(email);
	    boolean matchFound = m.matches();

	    if (!matchFound)
	    {
	    	check[1] = "Invalid Email";
	    }
	    if(text == null || (text != null && text.equals("")))
	    {
	    	check[2] = "Message is not empty";
	    }
	    
		//check capchar
		String remoteAddr = req.getRemoteAddr();
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey("6LezlM0SAAAAACEvZbJ36aiWaONZ6PhnFFEs504l");

        String challenge = req.getParameter("recaptcha_challenge_field");
        String uresponse = req.getParameter("recaptcha_response_field");
        if(challenge == null)
        {
        	challenge = "b";
        }
        if(uresponse == null)
        {
        	uresponse = "a";
        }
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

        if (!reCaptchaResponse.isValid()) {
        	check[3] = "Invalid Capchar";
        }
		
        if(check[0] == null && check[1] == null && check[2] == null && check[3] == null)
		{
			try {
				Message msg = new MimeMessage(session);
				msg.setFrom(new InternetAddress("admin@showsiteinfo.com", email));
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
						"admin@showsiteinfo.com", email));
				msg.setSubject("Mail contact from " + name + "(domaintips)");
				msg.setText(msgBody);
				Transport.send(msg);
	
			} catch (AddressException e) {
				// ...
			} catch (MessagingException e) {
				// ...
			}
		}
        req.setAttribute("check", check);
        req.setAttribute("title", "Contact us");
		req.setAttribute("description", "Contact us  "+Utils.ValidDomain()+" - A Free SEO tool that provides free website analysis, traffic details, rankings, contact email ,similar site, same owner site , same ip site, optimization and promotion reports.");
		req.setAttribute("keyword", "contact,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info");
		
		try {
			req.getRequestDispatcher("/contact.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
